import Vue from 'vue'
import VueI18n from 'vue-i18n'

import EleEnLocale from 'element-ui/lib/locale/lang/en'
import EleZhLocale from 'element-ui/lib/locale/lang/zh-CN'

import localeLang from './en-zh'
import store from '@/store'

Vue.use(VueI18n)

let enLocale = {}
let zhLocale = {}

for (const section in localeLang) {
  const sectionItems = localeLang[section]
  enLocale[section] = {}
  zhLocale[section] = {}
  for (const item in sectionItems) {
    const itemVal = sectionItems[item]
    enLocale[section][item] = itemVal[0]
    zhLocale[section][item] = itemVal[1]
  }
}

const messages = {
  en: {
    ...EleEnLocale,
    ...enLocale
  },
  zh: {
    ...EleZhLocale,
    ...zhLocale
  }
}

const i18n = new VueI18n({
  locale: store.getters.language || 'zh',
  messages
})

export default i18n
